﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;

public class Map
{

    public int _width;
    public int _height;
    public GameObject[] _goes;
    public int _tileWidthCount;
    public int _tileHeightCount;
    public int _screenWidth;
    public int _screenHeight;

    /// <summary>
    /// 当前地图中心点，世界坐标
    /// </summary>
    public Vector3 _center;
    public Vector3 _cell;
    /// <summary>
    /// 当前地图中心点，逻辑坐标
    /// </summary>
    public Position _centerPos;

    public int _tileWidth;
    public int _tileHeight;

    public Map()
    {
        _screenWidth = 1000;
        _screenHeight = 600;

        _tileWidth = 512;
        _tileHeight = 320;
    }

    /// <summary>
    /// 像素单位转换到世界坐标系单位
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    public float PixelToWorld(float p)
    {
        return p * 2.0f / _screenHeight;
    } 

    /// <summary>
    /// 逻辑坐标转换到世界坐标系
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public Vector3 LogicToWorldPos(Position pos)
    {
        Vector3 res = new Vector3();
        res.x = PixelToWorld(pos.X);
        res.y = PixelToWorld(_height - pos.Y);
        return res;
    }

    /// <summary>
    /// 世界坐标系转换为逻辑坐标系
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public Position WorldToLogicPos(Vector3 pos)
    {
        Position res = new Position();
        return res;
    }

    /// <summary>
    /// 屏幕坐标系转换为逻辑坐标系
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public Position ScreenToLogicPos(Vector3 pos)
    {
        Position res = new Position();
        res.X = _centerPos.X - _screenWidth / 2 + (int)pos.x;
        res.Y = _centerPos.Y + _screenHeight / 2 - (int)pos.y;
        return res;
    }

    /// <summary>
    /// 屏幕坐标转换为世界坐标系
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public Vector3 ScreenToWorldPos(Vector3 pos)
    {
        Position res = ScreenToLogicPos(pos);
        return LogicToWorldPos(res);
    }

    public bool LoadFromFile(string strFile)
    {
        _tileWidthCount = 23;
        _tileHeightCount = 22;

        _goes = new GameObject[_tileWidthCount * _tileHeightCount];

        for (int i = 0; i < _tileHeightCount; i++)
            for (int j = 0; j < _tileWidthCount; j++)
            {
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
                DynamicLoadTexture dt = go.AddComponent<DynamicLoadTexture>();
                StringBuilder sb = new StringBuilder();
                sb.Append(strFile);
                sb.AppendFormat("{0}_{1}.jpg", j, i);
                dt.url = sb.ToString();
                
                _goes[i * _tileWidthCount + j] = go;                
            }

        //
        _width = _tileWidthCount * 512;
        _height = _tileHeightCount * 320;
        _centerPos = new Position(_width / 2, _height / 2);
        _center = LogicToWorldPos(_centerPos);
        
        //

        for (int i = 0; i < _tileHeightCount; i++)
            for (int j = 0; j < _tileWidthCount; j++)
            {
                GameObject go = _goes[i * _tileWidthCount + j];
                Vector3 wp = LogicToWorldPos(new Position(j * _tileWidth + _tileWidth / 2,
                    i * _tileHeight + _tileHeight / 2));
                go.transform.Translate(wp);
                initTile(go);
            }

        return true;
    }

    private void initTile(GameObject go)
    {      

        MeshFilter meshFilter = go.GetComponent<MeshFilter>();
        //Object.Destroy(meshFilter);
        //meshFilter = null;
        if (meshFilter == null) {
            meshFilter = go.AddComponent<MeshFilter>();
            MeshRenderer meshRender = go.AddComponent<MeshRenderer>();
            Debug.Log("mesh filter is null");
            //
        }

        Mesh mesh = meshFilter.mesh;

        mesh.Clear();

        int verticesCount = 4;
        Vector3[] vertices = new Vector3[verticesCount];
        int trianglesCount = verticesCount - 2;
        int[] triangles = new int[verticesCount * 3];

        Position cellV = new Position(_tileWidth, _tileHeight);
        float glWidth = PixelToWorld(_tileWidth);
        float glHeight = PixelToWorld(_tileHeight);

        vertices[0] = new Vector3(-glWidth/2, -glHeight/2, 0f);
        vertices[1] = new Vector3(-glWidth/2, glHeight/2, 0f);
        vertices[2] = new Vector3(glWidth/2, -glHeight/2, 0f);
        vertices[3] = new Vector3(glWidth/2, glHeight/2, 0f);
        mesh.vertices = vertices;

        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;
        triangles[3] = 1;
        triangles[4] = 3;
        triangles[5] = 2;
        mesh.triangles = triangles;


        mesh.uv = new Vector2[]{new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 0), new Vector2(1, 1)};
        mesh.normals = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up};

    }
}
